package com.mysoft.gfyx.clue.service;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.mysoft.sdk.tenantplatform.client.PlatformTenantInfo;
import com.mysoft.sdk.tenantplatform.exception.TenantPlatformException;
import com.mysoft.sdk.tenantplatform.tianji.entity.ClientResponse;
import com.mysoft.sdk.tenantplatform.tianji.properties.TenantPlatformTianjiProperties;
import com.mysoft.sdk.tenantplatform.tianji.provider.TianjiTenantPlatformProvider;
import com.mysoft.sdk.tenantplatform.tianji.util.RestTemplateUtil;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;

import java.util.HashMap;
import java.util.Map;

public class TenantPlatformProviderService extends TianjiTenantPlatformProvider {

    private final TenantPlatformTianjiProperties tenantPlatformProperties;

    public TenantPlatformProviderService(TenantPlatformTianjiProperties tenantPlatformProperties, TenantPlatformTianjiProperties tenantPlatformProperties1) {
        super(tenantPlatformProperties);
        this.tenantPlatformProperties = tenantPlatformProperties1;
    }

    private PlatformTenantInfo getTenantInfoByCode(String tenantCode) {
        //PlatformTenantInfo platformTenantInfo = (PlatformTenantInfo)this.tenantManageCache.get(tenantCode);
        PlatformTenantInfo platformTenantInfo = null;
        if (null == platformTenantInfo) {
            String endpoint = String.valueOf(this.tenantPlatformProperties.getEndpoint());
            if (CharSequenceUtil.isEmpty(endpoint)) {
                throw new TenantPlatformException("不存在租户平台端点配置，请检查是否漏配!");
            }

            ParameterizedTypeReference<ClientResponse<PlatformTenantInfo>> responseType = new ParameterizedTypeReference<ClientResponse<PlatformTenantInfo>>() {
            };
            Map<String, Object> params = this.generateSign();
            params.put("tenantCode", tenantCode);
            ResponseEntity<ClientResponse<PlatformTenantInfo>> result = RestTemplateUtil.post(RestTemplateUtil.buildUrl(endpoint + "/api/tenant/tenant-info", params), responseType, (LinkedMultiValueMap) null);
            if (!result.hasBody()) {
                throw new TenantPlatformException("请求租户信息失败!返回租户信息为空!");
            }

            ClientResponse<PlatformTenantInfo> response = (ClientResponse) result.getBody();
            if (response == null) {
                throw new TenantPlatformException("请求失败,返回值为空");
            }

            if (!response.isResult()) {
                throw new TenantPlatformException(response.getCode() + " -> " + response.getMessage());
            }

            platformTenantInfo = (PlatformTenantInfo) response.getData();
            if (null == platformTenantInfo) {
                throw new TenantPlatformException("租户信息为空");
            }

            if (!CollectionUtils.isEmpty(platformTenantInfo.getDbList())) {
                // this.tenantManageCache.put(tenantCode, platformTenantInfo);
            }
        }

        return platformTenantInfo;
    }

    private Map<String, Object> generateSign() {
        long requestTime = System.currentTimeMillis() / 1000L;
        String signId = this.tenantPlatformProperties.getSignId();
        String signKey = this.tenantPlatformProperties.getSignKey();
        if (!CharSequenceUtil.isEmpty(signId) && !CharSequenceUtil.isEmpty(signKey)) {
            Map<String, Object> params = new HashMap(4);
            params.put("code", signId);
            params.put("requestTime", String.valueOf(requestTime));
            params.put("sign", DigestUtil.md5Hex(signKey + requestTime));
            return params;
        } else {
            throw new TenantPlatformException("多租户系统接口签名id和签名key配置不能为空");
        }
    }
}
